package com.wing.purchase.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.purchase.model.entity.PurchaseType;
import com.wing.purchase.model.entity.ReturnGoodsType;
import com.wing.purchase.service.ReturnGoodsTypeService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 *  退货类型管理
 * @author
 */
@Validated
@Api(value = "Admin-退货类型管理", tags = "#/purchase/returnGoodsType")
@RestController("returnGoodsTypeController")
@RequestMapping("purchase/admin/returnGoodsType")
@RequiredArgsConstructor
public class ReturnGoodsTypeController {

    @Autowired
    private ReturnGoodsTypeService returnGoodsTypeService;


    @ApiOperation(value = "查询所有退货类型", notes = "搜索")
    @GetMapping("/list")
    public PageResult<ReturnGoodsType> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(defaultValue = "sort_number") String sort,
            @RequestParam(defaultValue = "asc") String order){
        Page<ReturnGoodsType> pg = new Page<>(page, limit);
        QueryWrapper<ReturnGoodsType> queryWrapper = new QueryWrapper<>();
        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            if (StringUtils.equals("asc", order)) {
                queryWrapper.orderByAsc(StrUtil.toUnderlineCase(sort));
            } else {
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(sort));
            }
        }
        IPage<ReturnGoodsType> list = returnGoodsTypeService.getBaseMapper().selectPage(pg, queryWrapper);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }



    @ApiOperation(value = "添加退货类型", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@RequestBody ReturnGoodsType returnGoodsType) {
        boolean save = returnGoodsTypeService.save(returnGoodsType);
        if (save) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }

    @ApiOperation(value = "修改退货类型", notes = "修改")
    @PostMapping("/update")
    public JsonResult update(@RequestBody ReturnGoodsType returnGoodsType) {
        ReturnGoodsType goodsType = returnGoodsTypeService.getById(returnGoodsType.getId());
        if(goodsType == null){
        return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(ReturnGoodsType.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(returnGoodsType, goodsType, copyOptions);
        goodsType.setUpdateTime(LocalDateTime.now());
        goodsType.setModifier(JwtUtils.getLoginUserId());
        goodsType.setModifierName(JwtUtils.getLoginUsername());
        if (returnGoodsTypeService.updateById(goodsType)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }

    @ApiOperation(value = "停用/启用-退货类型", notes = "停用/启用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "退货类型表id", required = true, dataType = "Integer", paramType = "path"),
            @ApiImplicitParam(name = "status", value = "状态", required = true, dataType = "String", paramType = "path")
    })
    @PutMapping("/disableOrEnable/{id}/{status}" )
    public JsonResult disableOrEnable(@PathVariable("id") Long id,@PathVariable("status") String status) {
        if (returnGoodsTypeService.disableOrEnable(id,status)) {
            return JsonResult.success("操作成功");
        }
        return JsonResult.fail("操作失败");
    }
}
